<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="padding:10px;">
<p>说明：
    <span>巧用策略+工厂模式代替if-else,  易于扩展，增加一个新的策略只需要添加一个具体的策略类即可，基本不需要改变原有的代码，系统中薪资变量和属性的获取运用了</span>
</p>
<br/>
<p>正例：</p>
<p>首先，定义一个接口：</p>
<div style="margin:10px;padding:10px;border:1px solid #000000;white-space: pre-line;">
<br/>    /**
<br/>    * @author WuMing
<br/>    */
<br/>    public interface UserPayService {
<br/>
<br/>    /**
<br/>    * 计算应付价格
<br/>    */
<br/>    BigDecimal pay(BigDecimal orderPrice);
<br/>
<br/>    }
</div>
<p>定义常量类：</p>
<div style="margin:10px;padding:10px;border:1px solid #000000;white-space: pre-line;">
<br/>    /**
<br/>    * @author WuMing
<br/>    */
<br/>    public interface UserPayConstant {
<br/>
<br/>    /**
<br/>    * 专属会员
<br/>    */
<br/>    String PARTICULAR_VIP = "ParticularlyVip";
<br/>
<br/>    /**
<br/>    * 超级会员
<br/>    */
<br/>    String SUPER_VIP = "SuperVip";
<br/>
<br/>    /**
<br/>    * 普通会员
<br/>    */
<br/>    String VIP = "Vip";
<br/>
<br/>    }
</div>
<p>接着定义几个策略类：</p>
<div style="margin:10px;padding:10px;border:1px solid #000000;white-space: pre-line;">
<br/>   /**
<br/>   * @author WuMing
<br/>   */
<br/>   @Service
<br/>   public class ParticularlyVipPayService implements UserPayService, InitializingBean {

<br/>       @Override
<br/>       public BigDecimal pay(BigDecimal orderPrice) {
<br/>           if (消费金额大于30元) {
<br/>               return 7折价格;
<br/>           }
<br/>       }

<br/>       @Override
<br/>       public void afterPropertiesSet() throws Exception {
<br/>       UserPayServiceStrategyFactory.register(UserPayConstant.PARTICULAR_VIP, this);
<br/>       }
<br/>   }

<br/>   @Service
<br/>   public class SuperVipPayService implements UserPayService, InitializingBean{

<br/>       @Override
<br/>       public BigDecimal pay(BigDecimal orderPrice) {
<br/>           return 8折价格;
<br/>       }

<br/>       @Override
<br/>       public void afterPropertiesSet() throws Exception {
<br/>           UserPayServiceStrategyFactory.register(UserPayConstant.SUPER_VIP, this);
<br/>       }
<br/>   }

<br/>   @Service
<br/>   public class VipPayService implements UserPayService, InitializingBean {

<br/>       @Override
<br/>       public BigDecimal pay(BigDecimal orderPrice) {
<br/>           if(该用户超级会员刚过期并且尚未使用过临时折扣){
<br/>               临时折扣使用次数更新();
<br/>               returen 8折价格;
<br/>           }
<br/>           return 9折价格;
<br/>       }

<br/>       @Override
<br/>       public void afterPropertiesSet() throws Exception {
<br/>           UserPayServiceStrategyFactory.register(UserPayConstant.VIP, this);
<br/>       }
<br/>   }

</div>
</body>
</html>